import os
import tos

from tos import DataTransferType

# 从环境变量获取 AK 和 SK 信息。
# ak = os.getenv('TOS_ACCESS_KEY')
# sk = os.getenv('TOS_SECRET_KEY')
# ak = "AKLTY2FmZmI5Y2JhYmZmNDc3MmJmOTU3Yjg5Mzk5Y2U2YWM"
# sk = "WVRVMVpEVmtOV1V6TWpRME5HWmxZVGxoTURCbU9HSXpNemRtTURNd1lUUQ=="


# your endpoint 和 your region 填写Bucket 所在区域对应的Endpoint。# 以华北2(北京)为例，your endpoint 填写 tos-cn-beijing.volces.com，your region 填写 cn-beijing。
endpoint = "tos-cn-guangzhou.ivolces.com"
region = "cn-guangzhou"
bucket_name = "dcp-upload-pro"
# 对象名称，例如 example_dir 下的 example_object.txt 文件，则填写为 example_dir/example_object.txt
# object_key = "tos://dcp-upload-pro/2025-03-22/01e00bdc-d0c6-4db5-b4a0-caae297e6fa5.jpg"
object_key = "2025-03-22/01e00bdc-d0c6-4db5-b4a0-caae297e6fa5.jpg"

file_name = "./1.jpg"

try:
    # 创建 TosClientV2 对象，对桶和对象的操作都通过 TosClientV2 实现
    client = tos.TosClientV2(ak, sk, endpoint, region)
    # 若 file_name 为目录则将对象下载到此目录下, 文件名为对象名
    client.get_object_to_file(bucket_name, object_key, file_name)
except tos.exceptions.TosClientError as e:
    # 操作失败，捕获客户端异常，一般情况为非法请求参数或网络异常
    print('fail with client error, message:{}, cause: {}'.format(e.message, e.cause))
except tos.exceptions.TosServerError as e:
    # 操作失败，捕获服务端异常，可从返回信息中获取详细错误信息
    print('fail with server error, code: {}'.format(e.code))
    # request id 可定位具体问题，强烈建议日志中保存
    print('error with request id: {}'.format(e.request_id))
    print('error with message: {}'.format(e.message))
    print('error with http code: {}'.format(e.status_code))
    print('error with ec: {}'.format(e.ec))
    print('error with request url: {}'.format(e.request_url))
except Exception as e:
    print('fail with unknown error: {}'.format(e))
except Exception as e:
    print('fail with unknown error: {}'.format(e))